﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;

public partial class RSS_DetailRSS : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string link = Request.QueryString["link"];
        string pattern1 = "(?i)</script></div>(?<Content>.*?)<div class=\"othernews-title fl\">";
        if (!IsPostBack)
            detail.InnerHtml = ExtractContent(link, pattern1).ToString(); 
    }
    public string ExtractContent(string link, string pattern)
    {
        string strHTML = LoadHTML(link);
        strHTML = Regex.Replace(strHTML, "\t|\r|\n", "");
        string extractedContent = "";
        Match match = Regex.Match(strHTML, pattern);
        if (match.Success)
        {
            extractedContent = match.Groups["Content"].ToString();
            extractedContent = extractedContent.Replace("/Files/", "http://vnexpress.net/Files/");
        }
        return extractedContent;
    }

    private string LoadHTML(string link)
    {
        string strContent = "";
        try
        {
            WebRequest objWebRequest = WebRequest.Create(link);

            objWebRequest.Credentials = CredentialCache.DefaultCredentials;

            WebResponse objWebResponse = objWebRequest.GetResponse();
            Stream receiveStream = objWebResponse.GetResponseStream();
            StreamReader readStream = new StreamReader(receiveStream, System.Text.Encoding.UTF8);

            strContent = readStream.ReadToEnd();
            objWebResponse.Close();
            readStream.Close();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
        return strContent;

    } 
}